<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <title>管理员列表</title>
    <link rel="stylesheet" th:href="@{/src/css/layui.css}">
</head>
<body>
<blockquote class="layui-elem-quote list_search">
    <div class="layui-inline" shiro:lacksPermission="sys:admin:save">
        <a class="layui-btn layui-btn-normal adminAdd_btn layui-btn-disabled"><i
                class="layui-icon">&#xe608;</i> 添加管理员</a>
    </div>
    <div class="layui-inline" shiro:hasPermission="sys:admin:save">
        <a class="layui-btn layui-btn-normal adminAdd_btn"><i
                class="layui-icon">&#xe608;</i> 添加管理员</a>
    </div>
    <div class="layui-inline" shiro:lacksPermission="sys:admin:delete">
        <a class="layui-btn layui-btn-danger batchDel layui-btn-disabled"><i
                class="layui-icon">&#xe640;</i>批量删除</a>
    </div>
    <div class="layui-inline" shiro:hasPermission="sys:admin:delete">
        <a class="layui-btn layui-btn-danger batchDel"><i
                class="layui-icon">&#xe640;</i>批量删除</a>
    </div>
</blockquote>
<!--管理员Id-->
<input type="hidden" value="18" id="adminId">
<table id="adminList" lay-filter="test"></table>


<script th:src="@{/src/layui.js}"></script>
<script>
    layui.use(['table', 'jquery', 'layer', 'form', 'laypage', 'laytpl'], function () {
        var table = layui.table,
            $ = layui.jquery,
            laypage = layui.laypage,
            layer = layui.layer,
            laytpl = layui.laytpl;
        table.render({
            id: 'adminList',
            elem: '#adminList',
            cellMinWidth: 80,
            url: '/admin/list', //请求加载数据的接口
            page: true,
            limit: 10,
            limits: [10, 20, 30],
            toolbar: true,
            cols: [
                [
                    {type: 'checkbox'},
                    {title: 'ID', field: 'id'},
                    {title: '登录名', field: 'username'},
                    {title: '全称', field: 'fullname'},
                    {title: '邮箱', field: 'email'},
                    {title: '性别', field: 'sex', templet: '#sexTml'},
                    {title: '出生日期', field: 'birthday'},
                    {title: '地址', field: 'address'},
                    {title: '联系方式', field: 'phone'},
                    {title: '角色', field: 'roleName'},
                    {title: '操作', field: 'operate', templet: '#barEdit'}
                ]
            ],
            parseData: function (result) {//result是服务器端响应的数据
                return {
                    code: result.code == 200 ? 0 : result.code(),
                    msg: result.message,
                    data: result.data.list,
                    count: result.data.total
                }
            }
        });
        //添加管理员
        $(".adminAdd_btn").click(function () {
            var index = layer.open({
                type: 2,
                title: '编辑管理员',
                content: '/admin/addAdmin'
            })
            //窗口大小改变的事件
            $(window).resize(function () {
                layer.full(index);
            })
            layer.full(index);
        })
        //批量删除
        $(".batchDel").click(function () {
            //获取选中的行
            var checkStatus = table.checkStatus('adminList');//table中定义的id
            var data = checkStatus.data;
            var adminId = $("#adminId").val();
            if (data.length == 0) {
                layer.msg('请选择数据', {icon: 5});
                return;
            }
            var flag = false;
            var keys = "";
            $.each(data, function (index, obj) { //jquery中的foreach函数
                if (obj.roleName == "超级管理员") {
                    flag = true;
                    layer.msg('超级管理员不能删除');
                    return;
                }
                if (obj.id == adminId) {
                    flag = true;
                    layer.msg('不能删除自己');
                    return;
                }
                keys += obj.id + ",";
            })
            if (flag) {
                return;
            }
            keys = keys.substring(0, keys.length - 1);
            layer.confirm('确认删除？', function (index) {
                $.ajax({
                    url: '/admin/delAdmin/' + keys,
                    type: 'delete',
                    dataType: 'json',
                    success: function (result) {
                        if (result.code == 200) {
                            layer.msg('删除成功', {icon: 1}, function () {
                                location.reload();//刷新数据
                            });
                        } else {
                            layer.msg('删除失败');
                        }
                    }
                });
                layer.close(index);//关闭当前提示框
            })

        });

        //监听行工具事件
        //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
        //obj.data //获得当前行数据
        //var layEvent = obj.event; //获得 lay-event 对应的值
        table.on('tool(test)', function (obj) {
            var event = obj.event;
            var data = obj.data;
            if (event === 'edit') { //执行编辑
                layer.open({
                    type: 2,
                    title: '编辑管理员',
                    area: ['400px', '500px'],
                    content: '/admin/editAdmin/' + data.id
                });
            } else if (event === 'del') {//执行删除
                var adminId = $("#adminId").val();
                if (data.roleName == "超级管理员") {
                    layer.msg('超级管理员不能删除');
                    return;
                }
                if (data.id == adminId) {
                    layer.msg('不能删除自己');
                    return;
                }

                layer.confirm('确认删除', function (index) {

                    $.ajax({
                        url: '/admin/deleteAdmin/' + data.id,
                        type: 'delete',
                        dataType: 'json',
                        success: function (result) {
                            if (result.code == 200) {
                                layer.msg("删除成功", {icon: 1});
                                location.reload();
                            } else {
                                layer.msg("删除失败", {icon: 5});
                            }
                        }
                    })

                    layer.close(index);
                })
            }
        })


    });

</script>

<script type="text/html" id="barEdit">
    <a class="layui-btn layui-btn-xs layui-btn-disabled" lay-event="edit" shiro:lacksPermission="sys:admin:update">编辑</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit" shiro:hasPermission="sys:admin:update">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs layui-btn-disabled" lay-event="del" shiro:lacksPermission="sys:admin:delete">删除</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" shiro:hasPermission="sys:admin:delete">删除</a>
</script>
<script type="text/html" id="sexTml">
    {{# if(d.sex==1){ }}
    <span>男</span>
    {{# }else{ }}
    <span>女</span>
    {{# } }}
</script>

</body>
</html>